package View;

import javax.swing.*;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComponent;
import javax.swing.LayoutStyle.ComponentPlacement;

import Controller.GerenciadorAplicacoes;
import Model.*;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class TelaItemEstoque extends JScrollPane{
	Item i;
	JScrollPane item;
	JPanel cadastrar;
	JButton cancelar, limpar, salvar;
	TelaEstoque TelaEstoque;
	TelaItemEstoque TelaItemEstoque;
	JComboBox subcategoria, categoria;
	JSpinner tqtdu, tqtd;
	JFormattedTextField tnome;
	JTextArea tmsn;
	static GerenciadorAplicacoes gapp;
	public TelaItemEstoque() {

		item = new JScrollPane();
		cadastrar = new JPanel();
		item.getViewport().setBackground(new Color(100, 175, 239));
		cadastrar.setBorder(BorderFactory.createTitledBorder("CADASTRAR ITEM"));
		item.setBackground(new Color(0, 175, 239));
		item.setViewportView(cadastrar);
		cadastrar.setBackground(null);
		
		salvar = new JButton("SALVAR");
		salvar.setBackground(new Color(255, 255, 255));
		salvar.setForeground(new Color(0, 175, 239));
		salvar.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				salvar();
				TelaAplicacoes.AbrirEstoque();
			}});
		
		cancelar = new JButton("CANCELAR");
		cancelar.setBackground(new Color(255, 255, 255));
		cancelar.setForeground(new Color(0, 175, 239));
		cancelar.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				TelaAplicacoes.AbrirEstoque();
			}});
		
		limpar = new JButton("LIMPAR");
		limpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tnome.setText("");
				tmsn.setText("");
			}
		});
		
		limpar.setBackground(new Color(255, 255, 255));
		limpar.setForeground(new Color(0, 175, 239));
		
		
		JLabel lblNome = new JLabel("NOME");
		lblNome.setHorizontalAlignment(SwingConstants.RIGHT);
		
		tnome = new JFormattedTextField();
		
		JLabel lblQuantidadeTotal = new JLabel("QUANTIDADE TOTAL");
		lblQuantidadeTotal.setHorizontalAlignment(SwingConstants.RIGHT);
		
		tqtd = new JSpinner();
		
		JLabel lblQuantidadeUsada = new JLabel("QUANTIDADE USADA");
		lblQuantidadeUsada.setHorizontalAlignment(SwingConstants.RIGHT);
		
		tqtdu = new JSpinner();
		
		JLabel lblMensagensAdicionais = new JLabel("MENSAGENS ADICIONAIS");
		lblMensagensAdicionais.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblCategoria = new JLabel("CATEGORIA");
		lblCategoria.setHorizontalAlignment(SwingConstants.RIGHT);
		
		categoria = new JComboBox();
		AtualizarCategoria();
		JLabel lblSubcategoria = new JLabel("SUB-CATEGORIA");
		lblSubcategoria.setHorizontalAlignment(SwingConstants.RIGHT);
		
		subcategoria = new JComboBox();
		AtualizarSubCategoria();
		JScrollPane scrollPane = new JScrollPane();
		
		tmsn = new JTextArea();
		scrollPane.setViewportView(tmsn);
		GroupLayout gl_cadastrar = new GroupLayout(cadastrar);
		gl_cadastrar.setHorizontalGroup(
			gl_cadastrar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(13)
					.addComponent(lblCategoria, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(categoria, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(10)
					.addComponent(lblSubcategoria, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(subcategoria, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(10)
					.addComponent(lblNome, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(tnome, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(10)
					.addComponent(lblQuantidadeTotal, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(tqtd, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(13)
					.addComponent(lblQuantidadeUsada, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(tqtdu, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(13)
					.addComponent(lblMensagensAdicionais, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addGap(73)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(132)
					.addComponent(cancelar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(limpar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(salvar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
		);
		gl_cadastrar.setVerticalGroup(
			gl_cadastrar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(17)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(3)
							.addComponent(lblCategoria))
						.addComponent(categoria, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(3)
							.addComponent(lblSubcategoria))
						.addComponent(subcategoria, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNome))
						.addComponent(tnome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(33)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(3)
							.addComponent(lblQuantidadeTotal))
						.addComponent(tqtd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(3)
							.addComponent(lblQuantidadeUsada))
						.addComponent(tqtdu, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(33)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addGap(6)
							.addComponent(lblMensagensAdicionais))
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING)
						.addComponent(cancelar)
						.addComponent(limpar)
						.addComponent(salvar)))
		);
		cadastrar.setLayout(gl_cadastrar);
	}
	
	
	
	 public static ArrayList<Sub_Categoria> BuscarSubCategoria(){
		   gapp = new GerenciadorAplicacoes();
	   		ArrayList<Sub_Categoria> lista = gapp.buscarSubCategoria();
	   		
	   		return lista;
	   	}
	
		public void AtualizarSubCategoria(){
			ArrayList<Sub_Categoria> al = BuscarSubCategoria();
			String[] dados = new String[al.size()]; 
			for (int i=0;i<al.size();i++){
				
				Sub_Categoria sc = al.get(i);
				dados[i] = sc.nome;
			}
			DefaultComboBoxModel combo = new DefaultComboBoxModel(dados);
			subcategoria.setModel(combo);
			}
			
			
	   public static ArrayList<Categoria> BuscarCategoria(){
		   gapp = new GerenciadorAplicacoes();
	   		ArrayList<Categoria> lista = gapp.buscarCategoria();
	   		
	   		return lista;
	   	}

	   
	public void AtualizarCategoria(){
		ArrayList<Categoria> al = BuscarCategoria();
		String[] dados = new String[al.size()]; 
		for (int i=0;i<al.size();i++){
			
			TelaCategoriaEstoque.c = al.get(i);
			dados[i] = TelaCategoriaEstoque.c.nome;
		}
		
		DefaultComboBoxModel combo = new DefaultComboBoxModel(dados);
		categoria.setModel(combo);
		}
	
	
	
	public Item getItem(){
		i = new Item();
		Categoria c  = new Categoria();
		Sub_Categoria sc  = new Sub_Categoria();
		
		c.nome = categoria.getSelectedItem().toString();
		sc.nome = subcategoria.getSelectedItem().toString();
		
		gapp.pesquisarCategoria(c);
		gapp.pesquisarSubCategoria(sc);
		
		i.idcategoria = c.id;
		i.idsubcategoria = sc.id;
		i.nome = tnome.getText();
		i.msg = tmsn.getText();
		i.qtd_total = Integer.parseInt(tqtd.getValue().toString());
		i.qtd_usada = Integer.parseInt(tqtdu.getValue().toString());
		
		return i;
		
	}
	
	public void salvar(){
		gapp.cadastrarItem(getItem());
	}
	
	
	
	
	
	
}
